/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.application.isradeleon.notify;

import com.application.isradeleon.notify.helper.PixelMapHelper;
import com.application.isradeleon.notify.utils.HttpUtils;
import ohos.agp.utils.Color;
import ohos.app.Context;
import ohos.app.dispatcher.TaskDispatcher;
import ohos.app.dispatcher.task.TaskPriority;
import ohos.event.notification.NotificationHelper;
import ohos.event.notification.NotificationRequest;
import ohos.event.notification.NotificationRequest.NotificationContent;
import ohos.event.notification.NotificationRequest.NotificationPictureContent;
import ohos.event.notification.NotificationSlot;
import ohos.global.resource.NotExistException;
import ohos.global.resource.WrongTypeException;
import ohos.media.image.PixelMap;
import ohos.rpc.RemoteException;

import java.io.IOException;

/**
 * Notify通知管理
 */
public class Notify {
    public enum NotifyLevel {
        NONE,
        MIN,
        LOW,
        DEFAULT,
        HIGH
    }

    private final Context context;
    private int id = 1;
    private int level = NotificationSlot.LEVEL_DEFAULT;
    private String title = "Notify";
    private String text = "Notification test";
    private int color = -1;
    private boolean vibration = true;
    private boolean circle = false;
    private int smallIcon = -1;
    private Object largeIcon;
    private Object picture;
    private TaskDispatcher urlTask;
    private final NotificationSlot slot;
    private final NotificationRequest request;

    private Notify(Context _context) {
        this.context = _context;
        slot = new NotificationSlot("slot_notify_ohos_id", "slot_notify_ohos_name", level);
        request = new NotificationRequest(id);
        request.setSlotId(slot.getId());
    }

    public static Notify build(Context _context) {
        return new Notify(_context);
    }

    /**
     * 显示通知
     */
    public void show() {
        if (context == null) {
            return;
        }
        slot.setEnableVibration(vibration);
        try {
            NotificationPictureContent pc = new NotificationPictureContent();
            pc.setTitle(title);
            pc.setText(text);
            pc.setBriefText(text);
            showPicture(pc);
            showLargeIcon();
            NotificationContent content = new NotificationContent(pc);
            request.setContent(content);
            request.setDeliveryTime(System.currentTimeMillis());
            request.setShowDeliveryTime(true);
            int realColor = color == -1
                    ? Color.BLACK.getValue()
                    : context.getResourceManager().getElement(color).getColor();
            request.setColor(realColor);

            PixelMap smallMap = PixelMapHelper.getPixelMapFromRes(context, smallIcon);
            if (smallMap != null) {
                request.setLittleIcon(smallMap);
            }

            HttpUtils.closeStream();

            NotificationHelper.addNotificationSlot(slot);
            NotificationHelper.publishNotification(request);

        } catch (RemoteException | NotExistException | WrongTypeException | IOException e) {
            e.printStackTrace();
        }
    }

    private void showPicture(NotificationPictureContent pc) {
        if (picture != null) {
            if (picture instanceof String) {
                if (urlTask == null) {
                    urlTask = context.createParallelTaskDispatcher("", TaskPriority.DEFAULT);
                }
                urlTask.syncDispatch(() -> {
                    PixelMap pixelMap = PixelMapHelper.getPixelMapFromUrl(String.valueOf(picture));
                    pc.setBigPicture(pixelMap);
                });
            } else {
                if (picture instanceof Integer) {
                    PixelMap pixelMap = PixelMapHelper.getPixelMapFromRes(context, (int) picture);
                    pc.setBigPicture(pixelMap);
                }
            }
        }
    }

    private void showLargeIcon() {
        if (largeIcon != null) {
            if (largeIcon instanceof String) {
                if (urlTask == null) {
                    urlTask = context.createParallelTaskDispatcher("", TaskPriority.DEFAULT);
                }
                urlTask.syncDispatch(() -> {
                    PixelMap pixelMap = PixelMapHelper.getPixelMapFromUrl(String.valueOf(largeIcon));
                    if (circle && pixelMap != null) {
                        pixelMap = PixelMapHelper.toCircleBitmap(pixelMap);
                    }
                    request.setBigIcon(pixelMap);
                });
            } else {
                if (largeIcon instanceof Integer) {
                    PixelMap pixelMap = PixelMapHelper.getPixelMapFromRes(context, (int) largeIcon);
                    if (circle && pixelMap != null) {
                        pixelMap = PixelMapHelper.toCircleBitmap(pixelMap);
                    }
                    request.setBigIcon(pixelMap);
                }
            }
        }
    }

    public Notify setId(int id) {
        this.id = id;
        return this;
    }

    public Notify setLevel(NotifyLevel level) {
        switch (level) {
            case NONE:
                this.level = NotificationSlot.LEVEL_NONE;
                break;

            case MIN:
                this.level = NotificationSlot.LEVEL_MIN;
                break;

            case LOW:
                this.level = NotificationSlot.LEVEL_LOW;
                break;

            case DEFAULT:
                this.level = NotificationSlot.LEVEL_DEFAULT;
                break;

            case HIGH:
                this.level = NotificationSlot.LEVEL_HIGH;
                break;
        }
        return this;
    }

    public Notify setTitle(String title) {
        if (!title.isEmpty()) {
            this.title = title;
        }
        return this;
    }

    public Notify setText(String text) {
        if (!text.isEmpty()) {
            this.text = text;
        }
        return this;
    }

    public Notify enableVibration(boolean vibration) {
        this.vibration = vibration;
        return this;
    }

    public Notify largeCircle() {
        this.circle = true;
        return this;
    }

    public Notify setSmallIcon(int smallIcon) {
        this.smallIcon = smallIcon;
        return this;
    }

    public Notify setLargeIcon(int largeIcon) {
        this.largeIcon = largeIcon;
        return this;
    }

    public Notify setLargeIcon(String largeIconUrl) {
        this.largeIcon = largeIconUrl;
        return this;
    }

    public Notify setPicture(int bigPicture) {
        this.picture = bigPicture;
        return this;
    }

    public Notify setPicture(String bigPictureUrl) {
        this.picture = bigPictureUrl;
        return this;
    }

    public Notify setColor(int color) {
        this.color = color;
        return this;
    }

}
